package org.numb.controller;

import java.util.UUID;
import org.numb.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    /**
     * ModelAttribute的使用 <br/>
     * 方法一：方法参数上带Model参数，使用 model.addAttribute()设置
     *
     * @param model
     */
    @ModelAttribute
    public void addModel(Model model) {
        model.addAttribute("request_id", UUID.randomUUID().toString());
    }

    /**
     * ModelAttribute的使用 <br/>
     * 方法二：使用返回值，返回值会被自动调用设置进隐含的Model。 在Model中的key为返回值首字母小写，value为返回的值
     */
    @ModelAttribute
    public String addModel() {
        return UUID.randomUUID().toString();
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String helloUser(@RequestParam(value = "user_name", required = false) String userName,
        @ModelAttribute(value = "request_id") String requestId) {
        System.out.println(requestId);
        return "Hi, " + userName;
    }

    @RequestMapping("/hello/user")
    @ResponseBody
    public String helloUser(User user) {
        System.out.println("hello world");
        return "Hi, " + user.getName();
    }
}
